package dao;

import dao.inte.CommentRepository;
import domain.Comment;

import javax.inject.Singleton;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Singleton
public class CommentRepositoryImpl extends AbstractEntityRepository<Comment> implements CommentRepository {

    public CommentRepositoryImpl() {
        super(Comment.class);
    }

    @Override
    protected Class<Comment> getEntityClass() {
        return Comment.class;
    }

    @SuppressWarnings("unchecked")
    public List<Comment> findByProductId(Long productId) {
        return entityManager.createNamedQuery("Comment.getByProductId").setParameter("productId", productId).getResultList();
    }

    @SuppressWarnings("unchecked")
    public int countComments(Long productId) {
        Map map = new HashMap();
        map.put("id", productId);
        Long count = (Long) entityManager.createNamedQuery("Comment.countByProductId").setParameter("map", map).getSingleResult();

        return count.intValue();
    }
}
